#include <JXQCore/JXQGlobal>
#if defined(J_OS_UNIX) || defined(J_OS_LINUX)

#include "jcprocesswatcher_p.h"
#include <JXQIO/JProcess>
#include <wait.h>

namespace JXQ
{

void JListenThread::run()
{
	for(;;)
	{
		int status;
		pid_t pid = ::wait(&status);

		int err = errno;
		if( pid < 0 )
		{
			if( err != ECHILD )
				jError() << "JProcessWatcher: JListenThread: 'wiat' syscall error:" << strerror(err);
			break;
		}

		int ret = 0;
		if( not WIFEXITED(status) )
			ret = JProcess::ProcessCrashed;
		else
			ret = WEXITSTATUS(status);

		JListenThread::instance().notifiy(pid, ret);
	}
}

} //namespace JXQ


#endif //unix/linux
